//
//  LYShareView.swift
//  1010
//
//  Created by 米明 on 15/7/24.
//  Copyright (c) 2015年 米明. All rights reserved.
//

import UIKit

typealias BtnCallBack=(_ index:Int)->()

@available(iOS 8.0, *)
class LYShareView: UIView {
    
    var bg : UIVisualEffectView!
    var shareBtns : [UIButton]=[]
    var btnActionBack : BtnCallBack!
    
    
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        self.backgroundColor=UIColor.clear
        
        setUpBg()
        createShareBtns()
        show()
        
    }
    
    let BTN_WIDTH = CGFloat(50)
    let BTN_SPACE = CGFloat(40)
    
    func createShareBtns() {
        
        let images = ["star","sina","t"]
        
        for i in 0...2 {
            let btn = produceButton(frame: CGRect(x: 0, y: 0, width: BTN_WIDTH, height: BTN_WIDTH))!
            btn.center=CGPoint(x: SCREEN_SIZE.width/2.0+CGFloat(i-1)*(BTN_WIDTH+BTN_SPACE), y: SCREEN_SIZE.height+BTN_WIDTH)
            btn.addTarget(self, action: #selector(shareAction(_:)), for: UIControl.Event.touchUpInside)
            btn.tag=i
            btn.setImage(UIImage(named: images[i]), for: UIControl.State.normal)
            shareBtns.append(btn)
        }
    }
    
    //传递函数
    func shareActionCallBack(btnCallBack:@escaping (_ index:Int)->()) {
        btnActionBack = nil
        btnActionBack = btnCallBack
    }
    
    @objc func shareAction(_ btn:UIButton) {
        if btnActionBack != nil {
            btnActionBack(btn.tag)
        }
    }
    
    //MARK: 生产button
    func produceButton(frame: CGRect) -> UIButton! {
        let btn = UIButton(type: UIButton.ButtonType.system)
        btn.frame=frame
        btn.layer.masksToBounds=true
        btn.layer.cornerRadius=SPACE
        btn.tintColor=UIColor.white
        self.addSubview(btn)
        return btn
    }
    
    func setUpBg() {
        bg=UIVisualEffectView(effect: UIBlurEffect(style: UIBlurEffect.Style.dark))
        bg?.frame=UIScreen.main.bounds
        bg.alpha=0.1
        self.addSubview(bg)
    }
    
    func show() {
        UIView.animate(withDuration: 0.3, animations: {
            self.bg.alpha=0.98
        }, completion: nil)
        
        //shareBtns
        var i = 0
        for btn in shareBtns {
            UIView.animate(withDuration: 0.3, delay: Double(i) * 0.1, options: [], animations: {
                btn.center.y -= 2*self.BTN_WIDTH
            }, completion: { (finished) -> Void in
                
            })
            i += 1
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.dismiss()
    }
    
    func dismiss() {
        UIView.animate(withDuration: 0.3, animations: {
            self.bg.alpha=0
        }) { (finished) -> Void in
            self.removeFromSuperview()
        }
        //shareBtns
        var i = 0
        for btn in shareBtns {
            UIView.animate(withDuration: 0.3, delay: Double(i) * 0.1, options: [], animations: {
                btn.center.y += 2*self.BTN_WIDTH
                }, completion: { (finished) -> Void in
                    
            })
            i += 1
        }
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}
